package com.jackstraw.jack_base.datasource.mybatis.conf;

import com.jackstraw.jack_base.util.SpringUtil;
import com.jackstraw.jack_base.util.condition.ConditionalOnHavingProperties;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.defaults.DefaultSqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.boot.autoconfigure.MybatisProperties;
import org.mybatis.spring.boot.autoconfigure.SpringBootVFS;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;

import javax.sql.DataSource;

@Slf4j
@Order(Ordered.HIGHEST_PRECEDENCE)
@Configuration
@MapperScan(basePackages = {"com.jackstraw.**.dao.database"})
public class MyBatisConfig {


    public MyBatisConfig(ApplicationContext applicationContext) {
        SpringUtil.setApplicationContextJack(applicationContext);
    }

    @Bean
    @Primary
    @ConfigurationProperties(prefix = "jack.datasource.mybatis")
    @ConditionalOnHavingProperties(prefix = "jack.datasource", value = "mybatis")
    public MybatisProperties mybatisProperties() {
        return new MybatisProperties();
    }

    @Bean
    public SqlSessionFactory sqlSessionFactory(DataSource dataSource, MybatisProperties mybatisProperties) {
        //设置默认的mapper文件路径
        String[] mapperLocations = mybatisProperties.getMapperLocations();
        if (mapperLocations == null || mapperLocations.length == 0) {
            mapperLocations = new String[]{"classpath*:/mapper/**/*Dao.xml", "classpath*:/mapper/**/*Mapper.xml"};
        }

        mybatisProperties.setMapperLocations(mapperLocations);
        SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
        factory.setDataSource(dataSource);
        factory.setTypeAliasesPackage(mybatisProperties.getTypeAliasesPackage());
        factory.setVfs(SpringBootVFS.class);
        // 设置mybatis的xml所在位置
        factory.setMapperLocations(mybatisProperties.resolveMapperLocations());
        factory.setConfiguration(mybatisProperties.getConfiguration());
        try {
            return factory.getObject();
        } catch (Exception e) {
            log.error("创建SqlSessionFactory异常，异常原因为：{}", e);
            return new DefaultSqlSessionFactory(mybatisProperties.getConfiguration());
        }
    }

    @Bean
    public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory,
                                                 MybatisProperties mybatisProperties) {
        ExecutorType executorType = mybatisProperties.getExecutorType();


        if (executorType != null) {
            return new SqlSessionTemplate(sqlSessionFactory, executorType);
        } else {
            return new SqlSessionTemplate(sqlSessionFactory);
        }
    }

}